home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / recno / rec_get.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  6.7 KB  |  303 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)rec_get.c    8.2 (Berkeley) 9/7/93";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  39. #include "OurMalloc.h"
  40. #endif
  41.  
  42. #include <sys/types.h>
  43.  
  44. #include <errno.h>
  45. #include <stddef.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #ifndef macintosh
  50. #include <unistd.h>
  51. #endif
  52.  
  53. #include <db.h>
  54. #include "recno.h"
  55.  
  56. /*
  57.  * __REC_GET -- Get a record from the btree.
  58.  *
  59.  * Parameters:
  60.  *    dbp:    pointer to access method
  61.  *    key:    key to find
  62.  *    data:    data to return
  63.  *    flag:    currently unused
  64.  *
  65.  * Returns:
  66.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  67.  */
  68. int
  69. __rec_get(dbp, key, data, flags)
  70.     const DB *dbp;
  71.     const DBT *key;
  72.     DBT *data;
  73.     u_int flags;
  74. {
  75.     BTREE *t;
  76.     EPG *e;
  77.     recno_t nrec;
  78.     int status;
  79.  
  80.     t = dbp->internal;
  81.  
  82.     /* Toss any page pinned across calls. */
  83.     if (t->bt_pinned != NULL) {
  84.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  85.         t->bt_pinned = NULL;
  86.     }
  87.  
  88.     /* Get currently doesn't take any flags, and keys of 0 are illegal. */
  89.     if (flags || (nrec = *(recno_t *)key->data) == 0) {
  90.         errno = EINVAL;
  91.         return (RET_ERROR);
  92.     }
  93.  
  94.     /*
  95.      * If we haven't seen this record yet, try to find it in the
  96.      * original file.
  97.      */
  98.     if (nrec > t->bt_nrecs) {
  99.         if (ISSET(t, R_EOF | R_INMEM))
  100.             return (RET_SPECIAL);
  101.         if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
  102.             return (status);
  103.     }
  104.  
  105.     --nrec;
  106.     if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
  107.         return (RET_ERROR);
  108.  
  109.     status = __rec_ret(t, e, 0, NULL, data);
  110.     if (ISSET(t, B_DB_LOCK))    
  111.         mpool_put(t->bt_mp, e->page, 0);
  112.      else
  113.         t->bt_pinned = e->page;
  114.     return (status);
  115. }
  116.  
  117. /*
  118.  * __REC_FPIPE -- Get fixed length records from a pipe.
  119.  *
  120.  * Parameters:
  121.  *    t:    tree
  122.  *    cnt:    records to read
  123.  *
  124.  * Returns:
  125.  *    RET_ERROR, RET_SUCCESS
  126.  */
  127. int
  128. __rec_fpipe(t, top)
  129.     BTREE *t;
  130.     recno_t top;
  131. {
  132.     DBT data;
  133.     recno_t nrec;
  134.     size_t len;
  135.     int ch;
  136.     char *p;
  137.  
  138.     data.data = t->bt_dbuf;
  139.     data.size = t->bt_reclen;
  140.  
  141.     if (t->bt_dbufsz < t->bt_reclen) {
  142.         if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
  143.             return (RET_ERROR);
  144.         t->bt_dbufsz = t->bt_reclen;
  145.     }
  146.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  147.         len = t->bt_reclen;
  148.         for (p = t->bt_dbuf;; *p++ = ch)
  149.             if ((ch = getc(t->bt_rfp)) == EOF || !len--) {
  150.                 if (__rec_iput(t, nrec, &data, 0)
  151.                     != RET_SUCCESS)
  152.                     return (RET_ERROR);
  153.                 break;
  154.             }
  155.         if (ch == EOF)
  156.             break;
  157.     }
  158.     if (nrec < top) {
  159.         SET(t, R_EOF);
  160.         return (RET_SPECIAL);
  161.     }
  162.     return (RET_SUCCESS);
  163. }
  164.  
  165. /*
  166.  * __REC_VPIPE -- Get variable length records from a pipe.
  167.  *
  168.  * Parameters:
  169.  *    t:    tree
  170.  *    cnt:    records to read
  171.  *
  172.  * Returns:
  173.  *    RET_ERROR, RET_SUCCESS
  174.  */
  175. int
  176. __rec_vpipe(t, top)
  177.     BTREE *t;
  178.     recno_t top;
  179. {
  180.     DBT data;
  181.     recno_t nrec;
  182.     indx_t len;
  183.     size_t sz;
  184.     int bval, ch;
  185.     char *p;
  186.  
  187.     bval = t->bt_bval;
  188.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  189.         for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) {
  190.             if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
  191.                 data.data = t->bt_dbuf;
  192.                 data.size = p - t->bt_dbuf;
  193.                 if (ch == EOF && data.size == 0)
  194.                     break;
  195.                 if (__rec_iput(t, nrec, &data, 0)
  196.                     != RET_SUCCESS)
  197.                     return (RET_ERROR);
  198.                 break;
  199.             }
  200.             if (sz == 0) {
  201.                 len = p - t->bt_dbuf;
  202.                 t->bt_dbufsz += (sz = 256);
  203.                 if ((t->bt_dbuf =
  204.                     realloc(t->bt_dbuf, t->bt_dbufsz)) == NULL)
  205.                     return (RET_ERROR);
  206.                 p = t->bt_dbuf + len;
  207.             }
  208.         }
  209.         if (ch == EOF)
  210.             break;
  211.     }
  212.     if (nrec < top) {
  213.         SET(t, R_EOF);
  214.         return (RET_SPECIAL);
  215.     }
  216.     return (RET_SUCCESS);
  217. }
  218.  
  219. /*
  220.  * __REC_FMAP -- Get fixed length records from a file.
  221.  *
  222.  * Parameters:
  223.  *    t:    tree
  224.  *    cnt:    records to read
  225.  *
  226.  * Returns:
  227.  *    RET_ERROR, RET_SUCCESS
  228.  */
  229. int
  230. __rec_fmap(t, top)
  231.     BTREE *t;
  232.     recno_t top;
  233. {
  234.     DBT data;
  235.     recno_t nrec;
  236.     caddr_t sp, ep;
  237.     size_t len;
  238.     char *p;
  239.  
  240.     sp = t->bt_cmap;
  241.     ep = t->bt_emap;
  242.     data.data = t->bt_dbuf;
  243.     data.size = t->bt_reclen;
  244.  
  245.     if (t->bt_dbufsz < t->bt_reclen) {
  246.         if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
  247.             return (RET_ERROR);
  248.         t->bt_dbufsz = t->bt_reclen;
  249.     }
  250.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  251.         if (sp >= ep) {
  252.             SET(t, R_EOF);
  253.             return (RET_SPECIAL);
  254.         }
  255.         len = t->bt_reclen;
  256.         for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++);
  257.         memset(p, t->bt_bval, len);
  258.         if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
  259.             return (RET_ERROR);
  260.     }
  261.     t->bt_cmap = sp;
  262.     return (RET_SUCCESS);
  263. }
  264.  
  265. /*
  266.  * __REC_VMAP -- Get variable length records from a file.
  267.  *
  268.  * Parameters:
  269.  *    t:    tree
  270.  *    cnt:    records to read
  271.  *
  272.  * Returns:
  273.  *    RET_ERROR, RET_SUCCESS
  274.  */
  275. int
  276. __rec_vmap(t, top)
  277.     BTREE *t;
  278.     recno_t top;
  279. {
  280.     DBT data;
  281.     caddr_t sp, ep;
  282.     recno_t nrec;
  283.     int bval;
  284.  
  285.     sp = t->bt_cmap;
  286.     ep = t->bt_emap;
  287.     bval = t->bt_bval;
  288.  
  289.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  290.         if (sp >= ep) {
  291.             SET(t, R_EOF);
  292.             return (RET_SPECIAL);
  293.         }
  294.         for (data.data = sp; sp < ep && *sp != bval; ++sp);
  295.         data.size = sp - (caddr_t)data.data;
  296.         if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
  297.             return (RET_ERROR);
  298.         ++sp;
  299.     }
  300.     t->bt_cmap = sp;
  301.     return (RET_SUCCESS);
  302. }
  303.